<template>
  <template>
      <view class="goods-list">
        <block v-for="(item, i) in goodsList" :key="i">
          <my-goods :goods="item"></my-goods>
        </block>
      </view>
  </template>
</template>

<script>
  export default {
    data() {
      return {
        queryObj:{
          query:'',
          cid:'',
          pagenum:1,
          pagesize:10
        },
        goodsList: [],
        total:0,
        isLoading:false
      };
    },
    onLoad(option) {
      this.queryObj.query = option.query || ''
      this.queryObj.cid = option.cid || ''
      this.getGoodsList()
    },
    methods:{
      async getGoodsList(cb){
        const {data:res} = await uni.$http.get('/goods/search',this.queryObj)
        if(res.meta.status !== 200) return this.$showMsg()
         cb && cb()
        this.goodsList = res.message.goods
        this.total = res.message.total
      },
      async getMoreGoods(){
        if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕')
        const {data:res} = await uni.$http.get('/goods/search',this.queryObj)
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.isLoading = false
      },
    },
    onReachBottom() {
      if(this.isLoading) return
      this.isLoading = true
      this.queryObj.pagenum += 1
      this.getMoreGoods()
    },
    onPullDownRefresh() {
      this.queryObj.pagenum = 1,
      this.goodsList = [],
      this.total = 0,
      this.isLoading = true
      
      this.getGoodsList(() => {uni.stopPullDownRefresh()})
    }
  }
</script>

<style lang="scss">
  
</style>
